from django.test import TestCase
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from .models import User


class UserModelTest(TestCase):
    """测试用户模型"""
    
    def setUp(self):
        self.user = User.objects.create_user(
            username='testuser',
            email='test@example.com',
            password='testpassword123',
            nickname='测试用户'
        )
    
    def test_user_creation(self):
        """测试用户创建"""
        self.assertEqual(self.user.username, 'testuser')
        self.assertEqual(self.user.email, 'test@example.com')
        self.assertTrue(self.user.check_password('testpassword123'))
        self.assertEqual(self.user.nickname, '测试用户')
        self.assertTrue(self.user.is_active)
        self.assertFalse(self.user.is_staff)


class UserAPITest(APITestCase):
    """测试用户API"""
    
    def setUp(self):
        # 创建测试用户
        self.user = User.objects.create_user(
            username='apiuser',
            email='api@example.com',
            password='apipassword123'
        )
        
        # API endpoints
        self.register_url = reverse('users:user_register')
        self.login_url = reverse('users:user_login')
        self.profile_url = reverse('users:user_profile')
    
    def test_user_register(self):
        """测试用户注册API"""
        data = {
            'username': 'newuser',
            'email': 'new@example.com',
            'password': 'newpassword123',
            'password2': 'newpassword123',
            'nickname': '新用户'
        }
        response = self.client.post(self.register_url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(User.objects.filter(username='newuser').exists())
    
    def test_user_login(self):
        """测试用户登录API"""
        data = {
            'username': 'apiuser',
            'password': 'apipassword123'
        }
        response = self.client.post(self.login_url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('data', response.data)
        self.assertIn('tokens', response.data['data'])
        self.assertIn('access', response.data['data']['tokens'])
        self.assertIn('refresh', response.data['data']['tokens'])
    
    def test_user_profile(self):
        """测试获取用户资料API"""
        # 先登录获取token
        login_data = {
            'username': 'apiuser',
            'password': 'apipassword123'
        }
        login_response = self.client.post(self.login_url, login_data, format='json')
        self.assertIn('data', login_response.data)
        self.assertIn('tokens', login_response.data['data'])
        self.assertIn('access', login_response.data['data']['tokens'])
        token = login_response.data['data']['tokens']['access']
        
        # 设置认证头
        self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {token}')
        
        # 请求用户资料
        response = self.client.get(self.profile_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('data', response.data)
        self.assertEqual(response.data['data']['username'], 'apiuser')
        self.assertEqual(response.data['data']['email'], 'api@example.com')


# 更多测试用例可以根据实际API添加
# 例如: 测试用户地址管理、密码修改等
